/**=============
 * 获取用户选择文字，并复制给range对象，
 * 获取用户选中的开始索引到结束索引
 * ================
 */
import { getActiveInput } from "./blur";
let range = new Range();
let first = true;
let textHtmlArray = []; //字符串转成带html的数组
//记录用户选中对象的参数
var rangeData = {
    start: 0,
    end: 0,
    startDom: "",
    endDom: "",
};
const selectChange = function() {
    console.log("range 开始监听");
    if (first) {
        document.addEventListener("selectionchange", function() {
            try {
                let setSelectionRange = window.getSelection().getRangeAt(0);
                //过滤下，如果已经闭合就消除缓存里面的数据
                if (setSelectionRange.collapsed) {
                    removeAllRanges();
                    return;
                }
                rangeData = {
                    text: "",
                    start: 0,
                    end: 0,
                    startDom: "",
                    endDom: "",
                };
                let commonAncestorContainer = setSelectionRange.commonAncestorContainer;

                console.log(
                    "range",
                    setSelectionRange,
                    "commonAncestorContainer.nodeType",
                    commonAncestorContainer && commonAncestorContainer.nodeType,
                    commonAncestorContainer &&
                    commonAncestorContainer.parentElement &&
                    commonAncestorContainer.parentElement.classList.contains(
                        "vue-keyboard-text-item"
                    )
                );
                //nodeType 1=>标签
                //nodeType 3=>文字
                //指定范围内的range才有效
                if (
                    commonAncestorContainer &&
                    ((commonAncestorContainer.nodeType == 1 &&
                            commonAncestorContainer.classList.contains(
                                "vue-keyboard-input-text"
                            )) ||
                        (commonAncestorContainer.nodeType == 3 &&
                            commonAncestorContainer.parentElement &&
                            commonAncestorContainer.parentElement.classList.contains(
                                "vue-keyboard-text-item"
                            )))
                ) {
                    rangeData.start = setSelectionRange.startOffset;
                    rangeData.end = setSelectionRange.endOffset;
                    rangeData.startDom = setSelectionRange.startContainer.parentElement;
                    rangeData.endDom = setSelectionRange.endContainer.parentElement;
                } else {
                    rangeData.start = -1;
                    rangeData.end = -1;
                    rangeData.startDom = null;
                    rangeData.endDom = null;
                }
            } catch (e) {
                console.warn(e);
            }
        });
    }
    first = false;
};
//startContainer
//endContainer
const getRange = function() {
    return range;
};
export const getStartAndEndIndex = function() {
    let { dom, vm } = getActiveInput();
    console.log("range prev", textHtmlArray.length, dom);
    if (
        Array.isArray(textHtmlArray) &&
        textHtmlArray.length &&
        dom &&
        rangeData.startDom
    ) {
        let parentsArr = Array.from(
            dom.querySelector(".vue-keyboard-input-text").children
        );
        console.log(
            "range 景来了",
            rangeData,
            rangeData.startDom,
            rangeData.startDom.getAttribute
        );
        return {
            start: parentsArr.findIndex(
                (el) =>
                el.getAttribute("index") == rangeData.startDom.getAttribute("index")
            ),
            end: parentsArr.findIndex(
                (el) =>
                el.getAttribute("index") == rangeData.endDom.getAttribute("index")
            ),
        };
    } else {
        return {
            start: -1,
            end: -1,
        };

        //collapsed: range.collapsed,
    }
};
//更新输入框内容列表
export const updateHtmlArr = function(arr) {
    textHtmlArray = arr;
};
export var removeAllRanges = function() {
    try {
        window.getSelection().removeAllRanges();
    } catch (e) {
        console.warn(e);
    }
};

selectChange();